package com.ruiben.mall.impl;

import com.ruiben.mall.IOrderBackService;
import com.ruiben.mall.mapper.UserBackProductMapper;
import com.ruiben.mall.mapper.UserOrderBackMapper;
import com.ruiben.mall.mapper.UserOrderMapper;
import com.ruiben.mall.pojo.UserOrder;
import com.ruiben.mall.pojo.UserOrderBack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class OrderBackServiceImpl implements IOrderBackService {

    @Autowired
    private UserOrderBackMapper orderBackMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private UserBackProductMapper userBackProductMapper;

    @Override
    public  List<UserOrderBack> getOrderBackDetail(String usrId) {
        Example example = new Example(UserOrderBack.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userid",usrId);
        List<UserOrderBack> userOrderBacks = orderBackMapper.selectByExample(example);
        if (userOrderBacks!=null&&userOrderBacks.size()>0){
            for (UserOrderBack orderBack:userOrderBacks){
                String orderid = orderBack.getOrderid();
                UserOrder orderDetail = userOrderMapper.getOrderDetail(orderid);
                orderBack.setUserOrder(orderDetail);
            }
        }

        return userOrderBacks;
    }


    @Override
    public  List<UserOrderBack> getReturnGoodsList(String usrId) {

        List<UserOrderBack> orderBackListByStatusAndUserId = orderBackMapper.getOrderBackListByStatusAndUserId(usrId, 0);

        return orderBackListByStatusAndUserId;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean delReturnGoodsOrder(String backId,String backGoodsId) {
        orderBackMapper.deleteByPrimaryKey(backId);
        userBackProductMapper.deleteByPrimaryKey(backGoodsId);

        return true;

    }
}
